package com.modbus.tool.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.modbus.tool.entity.dto.CollectedDataVO;
import com.modbus.tool.entity.po.CollectedData;
import com.modbus.tool.entity.vo.CollectedDataDTO;
import com.modbus.tool.mapper.CollectedDataMapper;
import com.modbus.tool.service.ICollectedDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;

import java.util.List;

import com.modbus.tool.common.Result;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Leno
 * @Date: 2025-08-07
 * @Description: CollectedDataServiceImpl
 */
@Service
@RequiredArgsConstructor
public class CollectedDataServiceImpl extends ServiceImpl<CollectedDataMapper, CollectedData> implements ICollectedDataService {

    private final CollectedDataMapper collectedDataMapper;

    @Override
    public Result<Page<CollectedData>> findPage(Integer pageNum, Integer pageSize, CollectedData collectedData) {
        Page<CollectedData> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CollectedData> lqw = new LambdaQueryWrapper<>();
        // 添加条件...

        return Result.success(page(page, lqw));
    }

    @Override
    public Result<Page<CollectedDataVO>> findPage(Integer pageNum, Integer pageSize, CollectedDataDTO collectedDataDTO) {
        Page<CollectedData> page = new Page<>(pageNum, pageSize);
        QueryWrapper<CollectedData> qw = new QueryWrapper<>();
        if (StrUtil.isNotBlank(collectedDataDTO.getDeviceName())) { // 模糊查询
            qw.like("d.name", collectedDataDTO.getDeviceName());
        }
        if (collectedDataDTO.getDeviceId() != null) {
            qw.eq("d.id", collectedDataDTO.getDeviceId());
        }
        qw.orderByDesc("c.collected_at");

        Page<CollectedDataVO> pageList = collectedDataMapper.findPage(page, qw);
        return Result.success(pageList);
    }

    @Override
    @Transactional
    public Result<String> deleteBatch(List<Long> ids) {
        removeByIds(ids);
        return Result.successMsg("删除成功");
    }

}

